﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using StudySystem.Windows;
using StudySystem.MainLogic.Items;
using StudySystem.MainLogic.FormControl;
using StudySystem.MainLogic;
using StudySystem.SQLServer;

namespace StudySystem
{
    public partial class Subject : UserControl
    {
        ClassSubject _sub;

        public ClassSubject Sub { get => _sub; set => _sub = value; }

        public Subject()
        {
            InitializeComponent();
        }

        public Subject(ClassSubject subject)
        {
            InitializeComponent();
            this.Sub = subject;
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            //Button b = (Button)sender;
            DialogResult dr = MessageBox.Show("确定删除学科：" + Sub.SubjectName.ToString() + "(包括其所有的问题)!",
                "warning！", MessageBoxButtons.OKCancel);
            if(dr == DialogResult.OK)
            {
                FormSubControl.DeleteASubject((FlowLayoutPanel)Parent, this);
                Sub.DeleteASubject(UserManagement.subjects);
            }
            
            //FlowLayoutPanel f = (FlowLayoutPanel)b.Parent.Parent;
            //b.Parent.Parent.Controls.Remove(b.Parent);
            //b.Parent.Dispose();
        }


        public void SetLblCount(string count)
        {
            lblCount.Text = "选择" + count + "道题";
        }

        private void btnModifyName_Click(object sender, EventArgs e)
        {
            int subjectID = Convert.ToInt32(Sub.SubjectID);
            FormSubName subName = new FormSubName(Sub, this);
            subName.Show();
        }

        private void btnOpenSub_Click(object sender, EventArgs e)
        {
            List<ClassQuestion> questions = Sub.GetAllQuestion();
            FormQuestions formQues = new FormQuestions(Sub);
            formQues.Show();
        }

        /// <summary>
        /// 给各个控件的txt赋值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Subject_Load(object sender, EventArgs e)
        {
            lblSubName.Text = Sub.SubjectName;
            SetSubjectCount(Sub.QuesCount.ToString());
            SetLblCount("0");
            trackBarCount.Maximum = Sub.QuesCount;
        }

        public void RenewSubject()
        {
            lblSubName.Text = Sub.SubjectName;
            SetSubjectCount(Sub.QuesCount.ToString());
            SetLblCount("0");
            trackBarCount.Value = 0;
            trackBarCount.Maximum = Sub.QuesCount;
        }

        private void trackBarCount_Scroll(object sender, EventArgs e)
        {

        }

        private void trackBarCount_ValueChanged(object sender, EventArgs e)
        {
            SetLblCount(trackBarCount.Value.ToString());
        }

        private void btnBatch_Click(object sender, EventArgs e)
        {
            DialogResult messDR = MessageBox.Show("使用Excel文件批量添加学科问题。请将数据放到第一个sheet中，问题放在第一列，答案放在第二列！",Sub.SubjectName);
            if(messDR == DialogResult.OK)
            {
                OpenFileDialog openFD = new OpenFileDialog();
                if(openFD.ShowDialog() == DialogResult.OK)
                {
                    DataTable dt = ClassQuestion.ExcelToQuestionTable(openFD.FileName,null);
                    ClassQuestion.BulkInsertQuestions(dt, Sub.SubjectID);
                }
            }
        }
    }
}
